package com.waypolice.manager.service.impl;

import com.waypolice.common.pojo.SearchResult;
import com.waypolice.manager.service.ArcSearchService;
import com.waypolice.mapper.*;
import com.waypolice.mapper.handwriting.CmsJudItemMapperSon;
import com.waypolice.mapper.handwriting.CmsJudVolumeMapperSon;
import com.waypolice.mapper.handwriting.CmsSinItemMapperSon;
import com.waypolice.pojo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

/**
 * @author zhangruyi
 * @create 2019-03-18 16:24
 * @desc
 **/
@Service
public class ArcSearchServiceImpl implements ArcSearchService {

    @Autowired
    private CmsJudVolumeMapper cmsJudVolumeMapper;

    @Autowired
    private CmsJudItemMapper cmsJudItemMapper;

    @Autowired
    private CmsJudVolumeMapperSon cmsJudVolumeMapperSon;

    @Autowired
    private CmsJudItemMapperSon cmsJudItemMapperSon;

    @Autowired
    private CmsSinItemMapperSon cmsSinItemMapperSon;

    @Autowired
    private CmsSinItemMapper cmsSinItemMapper;

    @Autowired
    private CmsBorRegisterMapper cmsBorRegisterMapper;


    @Autowired
    private CmsBorRegFileMapper cmsBorRegFileMapper;

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 根据条件查询卷
     **/
    @Override
    public List<CmsJudVolume> findAllVolume(List<CmsJudVolume> list, Integer scope) {
        List<CmsJudVolume> finalList = new ArrayList<>();
        CmsJudVolume volume = list.get(0);
        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        if (volume == null) {
            return null;
        }
        Boolean flag = false;
        //案卷类型
        if (!StringUtils.isBlank(volume.getVolClass())) {
            criteria.andVolClassEqualTo(volume.getVolClass());
            flag = true;
        }
        //保管期限
        if (!StringUtils.isBlank(volume.getVolStorageTime())) {
            criteria.andVolStorageTimeEqualTo(volume.getVolStorageTime());
            flag = true;
        }
        //案卷号
        if (!StringUtils.isBlank(volume.getVolNo())) {
            criteria.andVolNoEqualTo(volume.getVolNo());
            flag = true;
        }
        //案卷题名
        if (!StringUtils.isBlank(volume.getVolTitle())) {
            criteria.andVolTitleLike("%" + volume.getVolTitle() + "%");
            flag = true;
        }
        //归档年度
        if (!StringUtils.isBlank(volume.getVolArchiveYear())) {
            criteria.andVolArchiveYearEqualTo(volume.getVolArchiveYear());
            flag = true;
        }
        //归档单位
        if (!StringUtils.isBlank(volume.getVolArchiveUnit())) {
            criteria.andVolArchiveUnitEqualTo(volume.getVolArchiveUnit());
            flag = true;
        }
        //审计组长
        if (!StringUtils.isBlank(volume.getVolAuditLeader())) {
            criteria.andVolAuditLeaderEqualTo(volume.getVolAuditLeader());
            flag = true;
        }
        //密级
        if (!StringUtils.isBlank(volume.getVolSecret())) {
            criteria.andVolSecretEqualTo(volume.getVolSecret());
            flag = true;
        }
        //审计起止日期
        if (!StringUtils.isBlank(volume.getVolAuditDate())) {
            criteria.andVolAuditDateEqualTo(volume.getVolAuditDate());
            flag = true;
        }
        //归档日期
        if (!"".equals(volume.getVolArchiveDate()) && volume.getVolArchiveDate() != null) {
            criteria.andVolArchiveDateEqualTo(volume.getVolArchiveDate());
            flag = true;
        }
        //档号
        if (!StringUtils.isBlank(volume.getVolCaseNum())) {
            criteria.andVolCaseNumLike("%" + volume.getVolCaseNum() + "%");
            flag = true;
        }
        if(flag == false){
            return null;
        }
        List<Integer> lists = new ArrayList<>();
        lists.add(0);
        lists.add(1);
        lists.add(2);
        criteria.andVolStatusIn(lists);
        criteria.andVolArchiveStatusEqualTo(2);
        List<CmsJudVolume> rows = cmsJudVolumeMapper.selectByExample(cmsJudVolumeExample);
        for (CmsJudVolume cmsJudVolume : rows) {
            //判断用户配置的是公开检索还是按涉密检索
            if (scope == 1) {
                //查询出所有已归档的档案，取出公开的放入finalList
                if (cmsJudVolume.getVolPublicStatus() == 1) {
                    finalList.add(cmsJudVolume);
                }
            } else {
                //查询出所有已归档的档案，取出非涉密的放入finalList
                if ("".equals(cmsJudVolume.getVolSecret()) || cmsJudVolume.getVolSecret() == null) {
                    finalList.add(cmsJudVolume);
                }
            }
            //剩下未公开或者未涉密的，查询当前登录用户是否有资格查看，如果有，放入finalList
            Subject subject = SecurityUtils.getSubject();
            UpmsUser user = (UpmsUser) subject.getSession().getAttribute("loginUser");
            Integer userId = user.getUserId();
            CmsBorRegisterExample cmsBorRegisterExample = new CmsBorRegisterExample();
            CmsBorRegisterExample.Criteria criteria1 = cmsBorRegisterExample.createCriteria();
            criteria1.andBorUserIdEqualTo(Long.valueOf(userId));
            criteria1.andBorStatusEqualTo(1);
            criteria1.andTaskTypeEqualTo(3);
            List<CmsBorRegister> cmsBorRegisters = cmsBorRegisterMapper.selectByExample(cmsBorRegisterExample);
            for (CmsBorRegister cmsBorRegister : cmsBorRegisters) {
                Long id = cmsBorRegister.getId();
                CmsBorRegFileExample cmsBorRegFileExample = new CmsBorRegFileExample();
                CmsBorRegFileExample.Criteria criteria2 = cmsBorRegFileExample.createCriteria();
                criteria2.andBorRegIdEqualTo(id);
                criteria2.andFileShapeEqualTo("0");
                List<CmsBorRegFile> cmsBorRegFiles = cmsBorRegFileMapper.selectByExample(cmsBorRegFileExample);
                for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles) {
                    //用户所拥有权限的卷id，与未公开的比较，相同的话，放入finalList
                    Long fileId = cmsBorRegFile.getFileId();
                    if (fileId.equals(cmsJudVolume.getVolId())) {
                        finalList.add(cmsJudVolume);
                    }
                }
            }
        }
        return finalList;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 查询所有卷目录
     **/
    @Override
    public List<CmsJudVolume> findAllVolumeByAll(List<CmsJudVolume> list) {
        CmsJudVolume volume = list.get(0);
        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        if (volume == null) {
            return null;
        }
       // Boolean flag = false;
        //案卷类型
        if (!StringUtils.isBlank(volume.getVolClass())) {
            criteria.andVolClassEqualTo(volume.getVolClass());
         //   flag = true;
        }
        //保管期限
        if (!StringUtils.isBlank(volume.getVolStorageTime())) {
            criteria.andVolStorageTimeEqualTo(volume.getVolStorageTime());
          //  flag = true;
        }
        //案卷号
        if (!StringUtils.isBlank(volume.getVolNo())) {
            criteria.andVolNoEqualTo(volume.getVolNo());
          //  flag = true;
        }
        //案卷题名
        if (!StringUtils.isBlank(volume.getVolTitle())) {
            criteria.andVolTitleLike("%" + volume.getVolTitle() + "%");
          //  flag = true;
        }
        //归档年度
        if (!StringUtils.isBlank(volume.getVolArchiveYear())) {
            criteria.andVolArchiveYearEqualTo(volume.getVolArchiveYear());
          //  flag = true;
        }
        //归档单位
        if (!StringUtils.isBlank(volume.getVolArchiveUnit())) {
            criteria.andVolArchiveUnitEqualTo(volume.getVolArchiveUnit());
          //  flag = true;
        }
        //审计组长
        if (!StringUtils.isBlank(volume.getVolAuditLeader())) {
            criteria.andVolAuditLeaderEqualTo(volume.getVolAuditLeader());
          //  flag = true;
        }
        //密级
        if (!StringUtils.isBlank(volume.getVolSecret())) {
            criteria.andVolSecretEqualTo(volume.getVolSecret());
          //  flag = true;
        }
        //审计起止日期
        if (!StringUtils.isBlank(volume.getVolAuditDate())) {
            criteria.andVolAuditDateEqualTo(volume.getVolAuditDate());
          //  flag = true;
        }
        //归档日期
        if (!"".equals(volume.getVolArchiveDate()) && volume.getVolArchiveDate() != null) {
            criteria.andVolArchiveDateEqualTo(volume.getVolArchiveDate());
          //  flag = true;
        }
        //档号
        if (!StringUtils.isBlank(volume.getVolCaseNum())) {
            criteria.andVolCaseNumLike("%" + volume.getVolCaseNum() + "%");
           // flag = true;
        }
        /*if(flag == false){
            return null;
        }*/
        List<Integer> lists = new ArrayList<>();
        lists.add(0);
        lists.add(1);
        lists.add(2);
        criteria.andVolStatusIn(lists);
        criteria.andVolArchiveStatusEqualTo(2);
        List<CmsJudVolume> rows = cmsJudVolumeMapper.selectByExample(cmsJudVolumeExample);
        return rows;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 所有归档公开的件
     **/
    @Override
    public List<CmsJudItem> findAllItem(List<CmsJudItem> list) {
        CmsJudItem item = list.get(0);
        CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
        CmsJudItemExample.Criteria criteria = cmsJudItemExample.createCriteria();
        if (item == null) {
            return null;
        }
        //单元号
        if (!StringUtils.isBlank(item.getItemTemporary())) {
            criteria.andItemTemporaryEqualTo(item.getItemTemporary());
        }
        //发文日期
        if (!StringUtils.isBlank(item.getItemCreateTime())) {
            criteria.andItemCreateTimeEqualTo(item.getItemCreateTime());
        }
        //题名
        if (!StringUtils.isBlank(item.getItemTitle())) {
            criteria.andItemTitleLike("%" + item.getItemTitle() + "%");
        }
        //文号
        if (!StringUtils.isBlank(item.getItemName())) {
            criteria.andItemNameLike("%" + item.getItemName() + "%");
        }
        //责任者
        if (!StringUtils.isBlank(item.getItemRespons())) {
            criteria.andItemResponsLike("%" + item.getItemRespons() + "%");
        }
        //密级
        if (!StringUtils.isBlank(item.getItemSecret())) {
            criteria.andItemSecretEqualTo(item.getItemSecret());
        }
        //归档日期
        if (!"".equals(item.getItemStr11Date()) && item.getItemStr11Date() != null) {
            criteria.andItemStr11DateEqualTo(item.getItemStr11Date());
        }
        List<Long> lists = new ArrayList<>();
        lists.add(0L);
        lists.add(1L);
        lists.add(2L);
        criteria.andItemStatusIn(lists);
        List<CmsJudItem> rows = cmsJudItemMapper.selectByExample(cmsJudItemExample);
        List<CmsJudItem> items = new ArrayList<>();
        for (CmsJudItem cmsJudItem : rows) {
            Long itemId = cmsJudItem.getItemId();
            CmsJudItem cmsJudItem1 = cmsJudItemMapper.selectByPrimaryKey(itemId);
            Long itmeVolId = cmsJudItem1.getItmeVolId();
            CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(itmeVolId);
            //件关联的卷档案状态
            Integer volArchiveStatus = cmsJudVolume.getVolArchiveStatus();
            //件关联的卷是否公开
            Integer volPublicStatus = cmsJudVolume.getVolPublicStatus();
            //归档已公开的件
            if (volArchiveStatus == 2 && volPublicStatus == 1) {
                items.add(cmsJudItem);
            }
        }
        return items;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 根据条件查询卷内件
     **/
    @Override
    public List<CmsJudItem> findAllVolumeAndItem(List<CmsJudVolume> vollist, List<CmsJudItem> itemlist, Integer scope) {
        List<CmsJudItem> finalList = new ArrayList<>();
        CmsJudVolume volume = vollist.get(0);
        CmsJudItem items = itemlist.get(0);
        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        if (volume == null) {
            return null;
        }
        //案卷类型
        if (!StringUtils.isBlank(volume.getVolClass())) {
            criteria.andVolClassEqualTo(volume.getVolClass());
        }
        //保管期限
        if (!StringUtils.isBlank(volume.getVolStorageTime())) {
            criteria.andVolStorageTimeEqualTo(volume.getVolStorageTime());
        }
        //案卷号
        if (!StringUtils.isBlank(volume.getVolNo())) {
            criteria.andVolNoEqualTo(volume.getVolNo());
        }
        //案卷题名
        if (!StringUtils.isBlank(volume.getVolTitle())) {
            criteria.andVolTitleLike("%" + volume.getVolTitle() + "%");
        }
        //归档年度
        if (!StringUtils.isBlank(volume.getVolArchiveYear())) {
            criteria.andVolArchiveYearEqualTo(volume.getVolArchiveYear());
        }
        //归档单位
        if (!StringUtils.isBlank(volume.getVolArchiveUnit())) {
            criteria.andVolArchiveUnitEqualTo(volume.getVolArchiveUnit());
        }
        //审计组长
        if (!StringUtils.isBlank(volume.getVolAuditLeader())) {
            criteria.andVolAuditLeaderEqualTo(volume.getVolAuditLeader());
        }
        //密级
        if (!StringUtils.isBlank(volume.getVolSecret())) {
            criteria.andVolSecretEqualTo(volume.getVolSecret());
        }
        //审计起止日期
        if (!StringUtils.isBlank(volume.getVolAuditDate())) {
            criteria.andVolAuditDateEqualTo(volume.getVolAuditDate());
        }
        //归档日期
        if (!"".equals(volume.getVolArchiveDate()) && volume.getVolArchiveDate() != null) {
            criteria.andVolArchiveDateEqualTo(volume.getVolArchiveDate());
        }
        //档号
        if (!StringUtils.isBlank(volume.getVolCaseNum())) {
            criteria.andVolCaseNumLike("%" + volume.getVolCaseNum() + "%");
        }
        List<Integer> lists = new ArrayList<>();
        lists.add(0);
        lists.add(1);
        lists.add(2);
        criteria.andVolStatusIn(lists);
        criteria.andVolArchiveStatusEqualTo(2);
        List<CmsJudVolume> allVolume = cmsJudVolumeMapper.selectByExample(cmsJudVolumeExample);
        //查询所有符合要求的卷，遍历取出卷内件
        if (!allVolume.isEmpty()) {
            for (CmsJudVolume cmsJudVolume : allVolume) {
                Long volId = cmsJudVolume.getVolId();
                CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
                CmsJudItemExample.Criteria criteria1 = cmsJudItemExample.createCriteria();
                criteria1.andItmeVolIdEqualTo(volId);
                List<CmsJudItem> itemlists = cmsJudItemMapper.selectByExample(cmsJudItemExample);
                //遍历卷内件
                for (CmsJudItem cmsJudItem : itemlists) {
                    if (items == null) {
                        return null;
                    }
                    //模糊查询件
                    CmsJudItemExample cmsJudItemExample1 = new CmsJudItemExample();
                    CmsJudItemExample.Criteria criteria2 = cmsJudItemExample1.createCriteria();
                    if (!StringUtils.isBlank(items.getItemTitle())) {
                        criteria2.andItemTitleLike("%" + items.getItemTitle() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemCreateTime())) {
                        criteria2.andItemCreateTimeLike("%" + items.getItemCreateTime() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemTemporary())) {
                        criteria2.andItemTemporaryLike("%" + items.getItemTemporary() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemName())) {
                        criteria2.andItemNameLike("%" + items.getItemName() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemSecret())) {
                        criteria2.andItemSecretLike("%" + items.getItemSecret() + "%");
                    }
                    List<CmsJudItem> items1 = cmsJudItemMapper.selectByExample(cmsJudItemExample1);
                    //遍历查询到的件
                    for (CmsJudItem cmsJudItem1 : items1) {
                        //判断用户配置的是公开检索还是按涉密检索
                        if (scope == 1) {
                            //查询出所有已归档的档案，取出公开的放入finalList
                            if ("1".equals(cmsJudItem1.getItemStr5())) {
                                //如果公开，判断公开的件的id和卷内件id,相同的话放入finalList
                                if (cmsJudItem1.getItemId().equals(cmsJudItem.getItemId())) {
                                    finalList.add(cmsJudItem);
                                }
                            }
                        } else {
                            //查询出所有已归档的档案，取出非涉密的放入finalList
                            if ("".equals(cmsJudItem1.getItemSecret()) || cmsJudItem1.getItemSecret() == null) {
                                //如果非涉密，判断公非涉密的件的id和卷内件id,相同的话放入finalList
                                if (cmsJudItem1.getItemId().equals(cmsJudItem.getItemId())) {
                                    finalList.add(cmsJudItem);
                                }
                            }
                        }
                        //如果未公开的，查询当前登录用户是否有资格查看，如果有，放入finalList
                        Subject subject = SecurityUtils.getSubject();
                        UpmsUser user = (UpmsUser) subject.getSession().getAttribute("loginUser");
                        Integer userId = user.getUserId();
                        CmsBorRegisterExample cmsBorRegisterExample = new CmsBorRegisterExample();
                        CmsBorRegisterExample.Criteria criteria3 = cmsBorRegisterExample.createCriteria();
                        criteria3.andBorUserIdEqualTo(Long.valueOf(userId));
                        criteria3.andBorStatusEqualTo(1);
                        criteria3.andTaskTypeEqualTo(3);
                        List<CmsBorRegister> cmsBorRegisters = cmsBorRegisterMapper.selectByExample(cmsBorRegisterExample);
                        for (CmsBorRegister cmsBorRegister : cmsBorRegisters) {
                            Long id = cmsBorRegister.getId();
                            CmsBorRegFileExample cmsBorRegFileExample = new CmsBorRegFileExample();
                            CmsBorRegFileExample.Criteria criteria4 = cmsBorRegFileExample.createCriteria();
                            criteria4.andBorRegIdEqualTo(id);
                            criteria4.andFileShapeEqualTo("1");
                            List<CmsBorRegFile> cmsBorRegFiles = cmsBorRegFileMapper.selectByExample(cmsBorRegFileExample);
                            for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles) {
                                //用户所拥有权限的卷id，与未公开的比较，相同的话，说明有权限
                                Long fileId = cmsBorRegFile.getFileId();
                                //比较有权限的件id和卷内件id,如果相同，放入finalList
                                if (fileId.equals(cmsJudItem1.getItemId())) {
                                    if (cmsJudItem1.getItemId().equals(cmsJudItem.getItemId())) {
                                        finalList.add(cmsJudItem);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return finalList;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 普通查询，根据条件查询所有档案
     **/
    @Override
    public SearchResult findAllVolumeAndItemByQw(String arcSearchMessage, Integer scope) {
        //1：先查询该用户有效的申请检索记录
        //剩下未公开的，查询当前登录用户是否有资格查看，如果有，放入finalList
        Subject subject = SecurityUtils.getSubject();
        UpmsUser user = (UpmsUser) subject.getSession().getAttribute("loginUser");
        Integer userId = user.getUserId();
        CmsBorRegisterExample cmsBorRegisterExample = new CmsBorRegisterExample();
        CmsBorRegisterExample.Criteria criteria1 = cmsBorRegisterExample.createCriteria();
        criteria1.andBorUserIdEqualTo(Long.valueOf(userId));
        criteria1.andBorStatusEqualTo(1);
        criteria1.andTaskTypeEqualTo(3);
        List<CmsBorRegister> cmsBorRegisters = cmsBorRegisterMapper.selectByExample(cmsBorRegisterExample);
        //2:根据用户输入条件查询卷
        //第一步：将查询出的卷公开的放入vol
        List<CmsJudVolume> vol = new ArrayList<>();
        List<CmsJudItem> ite = new ArrayList<>();
        List<CmsSinItem> SinI = new ArrayList<>();
        Map<String, Object> hashMap = new HashMap<String, Object>();
        if (!"".equals(arcSearchMessage)) {
            hashMap.put("arcSearchMessage", arcSearchMessage);
        }
        List<CmsJudVolume> volumes = cmsJudVolumeMapperSon.selectAllVolume(hashMap);
        //查询出所有已归档的档案，取出公开的放入finalList
        for (CmsJudVolume cmsJudVolume : volumes) {
            //判断用户配置的是公开检索还是按涉密检索
            if (scope == 1) {
                //查询出所有已归档的档案，取出公开的放入finalList
                if (cmsJudVolume.getVolPublicStatus() != null) {
                    if (cmsJudVolume.getVolPublicStatus() == 1) {
                        vol.add(cmsJudVolume);
                    }
                }
            } else {
                //查询出所有已归档的档案，取出非涉密的放入finalList
                if ("".equals(cmsJudVolume.getVolSecret()) || cmsJudVolume.getVolSecret() == null) {
                    vol.add(cmsJudVolume);
                }
            }
            //第二步：根据借阅申请表id查询借阅——用户关联表，查看该用户可以借阅的卷id
            for (CmsBorRegister cmsBorRegister : cmsBorRegisters) {
                Long id = cmsBorRegister.getId();
                CmsBorRegFileExample cmsBorRegFileExample = new CmsBorRegFileExample();
                CmsBorRegFileExample.Criteria criteria2 = cmsBorRegFileExample.createCriteria();
                criteria2.andBorRegIdEqualTo(id);
                criteria2.andFileShapeEqualTo("0");
                List<CmsBorRegFile> cmsBorRegFiles = cmsBorRegFileMapper.selectByExample(cmsBorRegFileExample);
                for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles) {
                    //第三步：将未公开的卷与用户所拥有权限的卷id作比较，如果相同，将未公开的卷放入finalList
                    Long fileId = cmsBorRegFile.getFileId();
                    if (fileId.equals(cmsJudVolume.getVolId())) {
                        vol.add(cmsJudVolume);
                    }
                }
            }
        }
        //3:根据用户输入条件查询卷内件
        //第一步：将查询出的件公开的放入ite
        List<CmsJudItem> items = cmsJudItemMapperSon.selectAllArchive(hashMap);
        for (CmsJudItem cmsJudItem : items) {
            Long itmeVolId = cmsJudItem.getItmeVolId();
            CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(itmeVolId);
            if (cmsJudVolume.getVolArchiveStatus() == 2) {
                //判断用户配置的是公开检索还是按涉密检索
                if (scope == 1) {
                    //查询出所有已归档的档案，取出公开的放入finalList
                    if ("1".equals(cmsJudItem.getItemStr5())) {
                        //如果公开，判断公开的件的id和卷内件id,相同的话放入finalList
                        if (cmsJudItem.getItemId().equals(cmsJudItem.getItemId())) {
                            ite.add(cmsJudItem);
                        }
                    }
                } else {
                    //查询出所有已归档的档案，取出非涉密的放入finalList
                    if ("".equals(cmsJudItem.getItemSecret()) || cmsJudItem.getItemSecret() == null) {
                        //如果非涉密，判断公非涉密的件的id和卷内件id,相同的话放入finalList
                        if (cmsJudItem.getItemId().equals(cmsJudItem.getItemId())) {
                            ite.add(cmsJudItem);
                        }
                    }
                }
                //第二步：根据借阅申请表id查询借阅——用户关联表，查看该用户可以借阅的卷内件id
                for (CmsBorRegister cmsBorRegister : cmsBorRegisters) {
                    Long id = cmsBorRegister.getId();
                    CmsBorRegFileExample cmsBorRegFileExample = new CmsBorRegFileExample();
                    CmsBorRegFileExample.Criteria criteria2 = cmsBorRegFileExample.createCriteria();
                    criteria2.andBorRegIdEqualTo(id);
                    criteria2.andFileShapeEqualTo("1");
                    List<CmsBorRegFile> cmsBorRegFiles = cmsBorRegFileMapper.selectByExample(cmsBorRegFileExample);
                    for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles) {
                        //第三步：将未公开的卷内件与用户所拥有权限的件id作比较，如果相同，将未公开的件放入ite
                        Long fileId = cmsBorRegFile.getFileId();
                        if (fileId.equals(cmsJudItem.getItemId())) {
                            ite.add(cmsJudItem);
                        }
                    }
                }
            }
        }
        //3:根据用户输入条件查询单件
        //第一步：将查询出的件公开的放入sinI
        List<CmsSinItem> cmsSinItems = cmsSinItemMapperSon.selectAllSinItem(hashMap);
        for (CmsSinItem cmsSinItem : cmsSinItems) {
            //判断用户配置的是公开检索还是按涉密检索
            if (scope == 1) {
                //查询出所有已归档的档案，取出公开的放入finalList
                if (cmsSinItem.getItemPublicStatus() != null) {
                    if(cmsSinItem.getItemPublicStatus() == 1 ){
                        SinI.add(cmsSinItem);
                    }
                }
            } else {
                //查询出所有已归档的档案，取出非涉密的放入finalList
                if ("".equals(cmsSinItem.getItemSecret()) || cmsSinItem.getItemSecret() == null) {
                    //如果非涉密，判断公非涉密的件的id和卷内件id,相同的话放入finalList
                    SinI.add(cmsSinItem);
                }
            }
            //第二步：根据借阅申请表id查询借阅——用户关联表，查看该用户可以借阅的单件id
            for (CmsBorRegister cmsBorRegister : cmsBorRegisters) {
                Long id = cmsBorRegister.getId();
                CmsBorRegFileExample cmsBorRegFileExample = new CmsBorRegFileExample();
                CmsBorRegFileExample.Criteria criteria2 = cmsBorRegFileExample.createCriteria();
                criteria2.andBorRegIdEqualTo(id);
                criteria2.andFileShapeEqualTo("2");
                List<CmsBorRegFile> cmsBorRegFiles = cmsBorRegFileMapper.selectByExample(cmsBorRegFileExample);
                for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles) {
                    //第三步：将未公开的件与用户所拥有权限的件id作比较，如果相同，将未公开的件放入sinI
                    Long fileId = cmsBorRegFile.getFileId();
                    if (fileId.equals(cmsSinItem.getItemId())) {
                        SinI.add(cmsSinItem);
                    }
                }
            }
        }
        SearchResult searchResult = new SearchResult();
        searchResult.setSinItemlist(SinI);
        searchResult.setItemlist(ite);
        searchResult.setVollist(vol);
        searchResult.setSearchVolNum(vol.size());
        searchResult.setSearchItemNum(ite.size());
        searchResult.setSearchSinItemNum(SinI.size());
        return searchResult;
    }

    @Override
    public List<CmsJudItem> selectItemAndVolumeByVolId(Long volId) {
        CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
        CmsJudItemExample.Criteria criteriaItem = cmsJudItemExample.createCriteria();
        cmsJudItemExample.setOrderByClause("\"ITEM_NUM\" ASC");
        List<Long> list1 = new ArrayList<>();
        list1.add(0L);
        list1.add(1L);
        list1.add(2L);
        criteriaItem.andItemStatusIn(list1);
        criteriaItem.andItmeVolIdEqualTo(volId);
        List<CmsJudItem> items = cmsJudItemMapper.selectByExample(cmsJudItemExample);
        return items;
    }

    @Override
    public List<CmsSinItem> findAllSinItem(List<CmsSinItem> list, Integer scope) {
        List<CmsSinItem> finalList = new ArrayList<>();
        CmsSinItem itemSin = list.get(0);
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria criteria = cmsSinItemExample.createCriteria();
        if (itemSin == null) {
            return null;
        }
        //单元号
        if (!StringUtils.isBlank(itemSin.getItemTemporary())) {
            criteria.andItemTemporaryEqualTo(itemSin.getItemTemporary());
        }
        //发文日期
        if (!StringUtils.isBlank(itemSin.getItemCreateTime())) {
            criteria.andItemCreateTimeEqualTo(itemSin.getItemCreateTime());
        }
        //题名
        if (!StringUtils.isBlank(itemSin.getItemTitle())) {
            criteria.andItemTitleLike("%" + itemSin.getItemTitle() + "%");
        }
        //密级
        if (!StringUtils.isBlank(itemSin.getItemSecret())) {
            criteria.andItemSecretEqualTo(itemSin.getItemSecret());
        }
        //文件类型
        if (!StringUtils.isBlank(itemSin.getItemExp1())) {
            criteria.andItemExp1EqualTo(itemSin.getItemExp1());
        }
        //保管期限
        if (!StringUtils.isBlank(itemSin.getItemNum())) {
            criteria.andItemNumEqualTo(itemSin.getItemNum());
        }
        //件号
        if (!StringUtils.isBlank(itemSin.getItemStr1())) {
            criteria.andItemStr1Like("%" + itemSin.getItemStr1() + "%");
        }
        //归档年度
        if (!StringUtils.isBlank(itemSin.getItemExp3())) {
            criteria.andItemExp3EqualTo(itemSin.getItemExp3());
        }
        List<Long> lists = new ArrayList<>();
        lists.add(0L);
        lists.add(1L);
        lists.add(2L);
        criteria.andItemStatusIn(lists);
        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        for (CmsSinItem cmsSinItem : cmsSinItems) {
            //判断用户配置的是公开检索还是按涉密检索
            if (scope == 1) {
                //查询出所有已归档的档案，取出公开的放入finalList
                if (cmsSinItem.getItemPublicStatus() == 1) {
                    finalList.add(cmsSinItem);
                }
            } else {
                //查询出所有已归档的档案，取出非涉密的放入finalList
                if ("".equals(cmsSinItem.getItemSecret()) || cmsSinItem.getItemSecret() == null) {
                    //如果非涉密，判断公非涉密的件的id和卷内件id,相同的话放入finalList
                    finalList.add(cmsSinItem);
                }
            }
            //剩下未公开的，查询当前登录用户是否有资格查看，如果有，放入finalList
            Subject subject = SecurityUtils.getSubject();
            UpmsUser user = (UpmsUser) subject.getSession().getAttribute("loginUser");
            Integer userId = user.getUserId();
            CmsBorRegisterExample cmsBorRegisterExample = new CmsBorRegisterExample();
            CmsBorRegisterExample.Criteria criteria1 = cmsBorRegisterExample.createCriteria();
            criteria1.andBorUserIdEqualTo(Long.valueOf(userId));
            criteria1.andBorStatusEqualTo(1);
            criteria1.andTaskTypeEqualTo(3);
            List<CmsBorRegister> cmsBorRegisters = cmsBorRegisterMapper.selectByExample(cmsBorRegisterExample);
            for (CmsBorRegister cmsBorRegister : cmsBorRegisters) {
                Long id = cmsBorRegister.getId();
                CmsBorRegFileExample cmsBorRegFileExample = new CmsBorRegFileExample();
                CmsBorRegFileExample.Criteria criteria2 = cmsBorRegFileExample.createCriteria();
                criteria2.andBorRegIdEqualTo(id);
                criteria2.andFileShapeEqualTo("2");
                List<CmsBorRegFile> cmsBorRegFiles = cmsBorRegFileMapper.selectByExample(cmsBorRegFileExample);
                for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles) {
                    //用户所拥有权限的卷id，与未公开的比较，相同的话，放入finalList
                    Long fileId = cmsBorRegFile.getFileId();
                    if (fileId.equals(cmsSinItem.getItemId())) {
                        finalList.add(cmsSinItem);
                    }
                }
            }
        }
        return finalList;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 查询所有卷内件
     **/
    @Override
    public List<CmsJudItem> findAllItemAll(List<CmsJudVolume> vollist, List<CmsJudItem> itemlist) {
        List<CmsJudItem> finalList = new ArrayList<>();
        CmsJudVolume volume = vollist.get(0);
        CmsJudItem items = itemlist.get(0);
        //第一步：根据条件模糊查询卷
        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        if (volume == null) {
            return null;
        }
        //案卷类型
        if (!StringUtils.isBlank(volume.getVolClass())) {
            criteria.andVolClassEqualTo(volume.getVolClass());
        }
        //保管期限
        if (!StringUtils.isBlank(volume.getVolStorageTime())) {
            criteria.andVolStorageTimeEqualTo(volume.getVolStorageTime());
        }
        //案卷号
        if (!StringUtils.isBlank(volume.getVolNo())) {
            criteria.andVolNoEqualTo(volume.getVolNo());
        }
        //案卷题名
        if (!StringUtils.isBlank(volume.getVolTitle())) {
            criteria.andVolTitleLike("%" + volume.getVolTitle() + "%");
        }
        //归档年度
        if (!StringUtils.isBlank(volume.getVolArchiveYear())) {
            criteria.andVolArchiveYearEqualTo(volume.getVolArchiveYear());
        }
        //归档单位
        if (!StringUtils.isBlank(volume.getVolArchiveUnit())) {
            criteria.andVolArchiveUnitEqualTo(volume.getVolArchiveUnit());
        }
        //审计组长
        if (!StringUtils.isBlank(volume.getVolAuditLeader())) {
            criteria.andVolAuditLeaderEqualTo(volume.getVolAuditLeader());
        }
        //密级
        if (!StringUtils.isBlank(volume.getVolSecret())) {
            criteria.andVolSecretEqualTo(volume.getVolSecret());
        }
        //审计起止日期
        if (!StringUtils.isBlank(volume.getVolAuditDate())) {
            criteria.andVolAuditDateEqualTo(volume.getVolAuditDate());
        }
        //归档日期
        if (!"".equals(volume.getVolArchiveDate()) && volume.getVolArchiveDate() != null) {
            criteria.andVolArchiveDateEqualTo(volume.getVolArchiveDate());
        }
        //档号
        if (!StringUtils.isBlank(volume.getVolCaseNum())) {
            criteria.andVolCaseNumLike("%" + volume.getVolCaseNum() + "%");
        }
        List<Integer> lists = new ArrayList<>();
        lists.add(0);
        lists.add(1);
        lists.add(2);
        criteria.andVolStatusIn(lists);
        criteria.andVolArchiveStatusEqualTo(2);
        List<CmsJudVolume> allVolume = cmsJudVolumeMapper.selectByExample(cmsJudVolumeExample);
        //查询所有符合要求的卷，遍历取出卷内件
        if (!allVolume.isEmpty()) {
            for (CmsJudVolume cmsJudVolume : allVolume) {
                Long volId = cmsJudVolume.getVolId();
                CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
                CmsJudItemExample.Criteria criteria1 = cmsJudItemExample.createCriteria();
                criteria1.andItmeVolIdEqualTo(volId);
                List<CmsJudItem> itemlists = cmsJudItemMapper.selectByExample(cmsJudItemExample);
                //遍历取出每个件
                for (CmsJudItem cmsJudItem : itemlists) {
                    if (items == null) {
                        return null;
                    }
                    //模糊查询件
                    CmsJudItemExample cmsJudItemExample1 = new CmsJudItemExample();
                    CmsJudItemExample.Criteria criteria2 = cmsJudItemExample1.createCriteria();
                    if (!StringUtils.isBlank(items.getItemTitle())) {
                        criteria2.andItemTitleLike("%" + items.getItemTitle() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemCreateTime())) {
                        criteria2.andItemCreateTimeLike("%" + items.getItemCreateTime() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemTemporary())) {
                        criteria2.andItemTemporaryLike("%" + items.getItemTemporary() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemName())) {
                        criteria2.andItemNameLike("%" + items.getItemName() + "%");
                    }
                    if (!StringUtils.isBlank(items.getItemSecret())) {
                        criteria2.andItemSecretLike("%" + items.getItemSecret() + "%");
                    }
                    List<CmsJudItem> items1 = cmsJudItemMapper.selectByExample(cmsJudItemExample1);
                    //遍历查询到的件
                    for (CmsJudItem cmsJudItem1 : items1) {
                        //将模糊查询到的件和模糊查询到的卷内件比较，提取交集，返回
                        if (cmsJudItem1.getItemId().equals(cmsJudItem.getItemId())) {
                            finalList.add(cmsJudItem);
                        }
                    }
                }
            }
        }
        return finalList;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 查询所有单件
     **/
    @Override
    public List<CmsSinItem> findAllSinItemAll(List<CmsSinItem> list) {
        CmsSinItem itemSin = list.get(0);
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria criteria = cmsSinItemExample.createCriteria();
        if (itemSin == null) {
            return null;
        }
        //单元号
        if (!StringUtils.isBlank(itemSin.getItemTemporary())) {
            criteria.andItemTemporaryEqualTo(itemSin.getItemTemporary());
        }
        //发文日期
        if (!StringUtils.isBlank(itemSin.getItemCreateTime())) {
            criteria.andItemCreateTimeEqualTo(itemSin.getItemCreateTime());
        }
        //题名
        if (!StringUtils.isBlank(itemSin.getItemTitle())) {
            criteria.andItemTitleLike("%" + itemSin.getItemTitle() + "%");
        }
        //密级
        if (!StringUtils.isBlank(itemSin.getItemSecret())) {
            criteria.andItemSecretEqualTo(itemSin.getItemSecret());
        }
        //文件类型
        if (!StringUtils.isBlank(itemSin.getItemExp1())) {
            criteria.andItemExp1EqualTo(itemSin.getItemExp1());
        }
        //保管期限
        if (!StringUtils.isBlank(itemSin.getItemNum())) {
            criteria.andItemNumEqualTo(itemSin.getItemNum());
        }
        //件号
        if (!StringUtils.isBlank(itemSin.getItemStr1())) {
            criteria.andItemStr1Like("%" + itemSin.getItemStr1() + "%");
        }
        //归档年度
        if (!StringUtils.isBlank(itemSin.getItemExp3())) {
            criteria.andItemExp3EqualTo(itemSin.getItemExp3());
        }
        List<Long> lists = new ArrayList<>();
        lists.add(0L);
        lists.add(1L);
        lists.add(2L);
        criteria.andItemStatusIn(lists);
        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        return cmsSinItems;
    }

    /**
     * @author zhangruyi
     * @create 2019-03-18 16:24
     * @desc 普通查询，查询所有档案
     **/
    @Override
    public SearchResult findAllVolumeAndItemByQwAll(String arcSearchMessage) {
        Map<String, Object> hashMap = new HashMap<String, Object>();

        List<CmsJudItem> itemss = new ArrayList<>();

        SearchResult searchResult = new SearchResult();
        if (!"".equals(arcSearchMessage)) {
            hashMap.put("arcSearchMessage", arcSearchMessage);
        }
        List<CmsJudVolume> volumes = cmsJudVolumeMapperSon.selectAllVolume(hashMap);
        if (!volumes.isEmpty()) {
            searchResult.setVollist(volumes);
        }

        List<CmsJudItem> items = cmsJudItemMapperSon.selectAllArchive(hashMap);
        if (!items.isEmpty()) {
            for (CmsJudItem cmsJudItem : items) {
                Long itmeVolId = cmsJudItem.getItmeVolId();
                CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(itmeVolId);
                if (cmsJudVolume.getVolArchiveStatus() == 2) {
                    itemss.add(cmsJudItem);
                }
                searchResult.setItemlist(itemss);
            }
        }
            List<CmsSinItem> cmsSinItems = cmsSinItemMapperSon.selectAllSinItem(hashMap);
            if (!cmsSinItems.isEmpty()) {
                searchResult.setSinItemlist(cmsSinItems);
            }
            searchResult.setSearchVolNum(volumes.size());
            searchResult.setSearchItemNum(itemss.size());
            searchResult.setSearchSinItemNum(cmsSinItems.size());
            return searchResult;
        }
    }